﻿using System;
using System.Data;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI.WebControls;
using HKB.Common;
using HKB.Web;
using HKB.Web.Admin;
using HKB.Web.DataHelper;
using HKB.Data;

public partial class Admin_CM_Ad_Manage : Admin, IRequiresSessionState
{
    private int _allCount = -1;
    private int _returnDataCountForEach = 10;
    private Shop_City _AD_Shop_City = null;
    protected DefaultProfile profile
    {
        get
        {
            return (DefaultProfile)this.Context.Profile;
        }
    }
    protected global_asax applicationInstance
    {
        get
        {
            return (global_asax)this.Context.ApplicationInstance;
        }
    }
    protected int AllCount
    {
        get
        {
            int allCount;
            if (this._allCount != -1)
            {
                allCount = this._allCount;
            }
            else
            {
                this._allCount = this.conn.ExecuteCount("select  count(*)   from CM_Advertisement\r\n                                            where AdLocation!='版位一' and AdLocation!='版位二' and AdLocation!='版位三' \r\n                                            and AdLocation!='版位四'  and IsDel=1");
                allCount = this._allCount;
            }
            return allCount;
        }
        set
        {
            this._allCount = value;
        }
    }
    protected int ReturnDataCountForEach
    {
        get
        {
            return this._returnDataCountForEach;
        }
        set
        {
            this._returnDataCountForEach = value;
        }
    }
    private Shop_City AD_Shop_City
    {
        get
        {
            Shop_City result;
            if (this._AD_Shop_City != null)
            {
                result = this._AD_Shop_City;
            }
            else
            {
                result = new Shop_City();
            }
            return result;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!base.IsPostBack)
        {
            this.GetAllProvince();
            this.DefaultValue();
            this.GetAllAd();
            this.GetPageLink();
        }
    }
    private void DefaultValue()
    {
        this.ddlSearchCity.Items.Insert(0, new ListItem("请选择...", ""));
        this.ddlSearchArea.Items.Insert(0, new ListItem("请选择...", ""));
    }
    private void GetAllProvince()
    {
        this.ddlProvince.DataSource = this.AD_Shop_City.GetAllProvince();
        this.ddlProvince.DataTextField = "Province";
        this.ddlProvince.DataValueField = "Province";
        this.ddlProvince.DataBind();
        this.ddlProvince.Items.Insert(0, new ListItem("请选择...", ""));
    }
    protected void ddlProvince_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.ddlProvince.SelectedIndex == 0)
        {
            this.ddlSearchCity.Items.Clear();
            this.ddlSearchCity.Items.Insert(0, new ListItem("请选择...", ""));
        }
        else
        {
            this.ddlSearchCity.DataSource = this.AD_Shop_City.GetCityByProvince(this.ddlProvince.SelectedValue);
            this.ddlSearchCity.DataTextField = "City";
            this.ddlSearchCity.DataValueField = "City";
            this.ddlSearchCity.DataBind();
            this.ddlSearchCity.Items.Insert(0, new ListItem("请选择...", ""));
            this.ddlSearchCity.SelectedIndex = 0;
        }
    }
    protected void ddlSearchCity_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.ddlSearchCity.SelectedIndex == 0)
        {
            this.ddlSearchArea.Items.Clear();
            this.ddlSearchArea.Items.Insert(0, new ListItem("请选择...", ""));
        }
        else
        {
            this.ddlSearchArea.DataSource = this.AD_Shop_City.GetAllAreaByCity(this.ddlSearchCity.SelectedValue);
            this.ddlSearchArea.DataTextField = "Area";
            this.ddlSearchArea.DataValueField = "Area";
            this.ddlSearchArea.DataBind();
            this.ddlSearchArea.Items.Insert(0, new ListItem("请选择...", ""));
            this.ddlSearchArea.SelectedIndex = 0;
        }
    }
    private void GetAllAd()
    {
        int currentPage = this.ValidateQueString();
        this.GridView1.DataSource = new CM_AD_Manage().GetAllAd(this.ReturnDataCountForEach, currentPage, this.QueryString());
        this.GridView1.DataBind();
    }
    protected void Button_Add_Click(object sender, EventArgs e)
    {
        base.Response.Redirect("CM_Ad_Update.aspx?act=add&MenuID=7&MenuName=服务&FileName=添加广告");
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        int currentPage = 1;
        this.GridView1.DataSource = new CM_AD_Manage().GetAllAd(this.ReturnDataCountForEach, currentPage, this.QueryString());
        this.GridView1.DataBind();
    }
    private string QueryString()
    {
        string text = "";
        if (!string.IsNullOrEmpty(this.txtAdName.Text.Trim()))
        {
            text = text + " and AdName like '%" + this.txtAdName.Text.Trim() + "%' ";
        }
        if (this.ddlProvince.SelectedIndex != 0)
        {
            text = text + " and Province ='" + this.ddlProvince.SelectedValue + "' ";
        }
        if (this.ddlSearchCity.SelectedIndex != 0)
        {
            text = text + " and City ='" + this.ddlSearchCity.SelectedValue + "' ";
        }
        if (this.ddlSearchArea.SelectedIndex != 0)
        {
            text = text + " and Area ='" + this.ddlSearchArea.SelectedValue + "' ";
        }
        return text;
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#E7EDFE'");
        e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c;");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (!base.admin.CheckAdminPower(3))
        {
            this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "alert", "<script>alert('对不起，你没有相应的权限,请跟系统超级管理员联系!');</script>");
        }
        else
        {
            string text = string.Empty;
            foreach (GridViewRow gridViewRow in this.GridView1.Rows)
            {
                CheckBox checkBox = (CheckBox)gridViewRow.Cells[0].FindControl("CheckBox1");
                if (checkBox.Checked)
                {
                    HiddenField hiddenField = (HiddenField)gridViewRow.Cells[0].FindControl("hfId");
                    text = text + hiddenField.Value + ",";
                }
            }
            if (text != string.Empty)
            {
                text = text.Substring(0, text.Length - 1);
                this.conn.Execute("UPDATE CM_Advertisement set Enabled=1  where AdID in (" + text + ")");
                this.GetAllAd();
            }
            else
            {
                this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "msg", "<script>alert('修改失败！');</script>");
            }
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        if (!base.admin.CheckAdminPower(3))
        {
            this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "alert", "<script>alert('对不起，你没有相应的权限,请跟系统超级管理员联系!');</script>");
        }
        else
        {
            string text = string.Empty;
            foreach (GridViewRow gridViewRow in this.GridView1.Rows)
            {
                CheckBox checkBox = (CheckBox)gridViewRow.Cells[0].FindControl("CheckBox1");
                if (checkBox.Checked)
                {
                    HiddenField hiddenField = (HiddenField)gridViewRow.Cells[0].FindControl("hfId");
                    text = text + hiddenField.Value + ",";
                }
            }
            if (text != string.Empty)
            {
                text = text.Substring(0, text.Length - 1);
                this.conn.Execute("UPDATE CM_Advertisement set Enabled=0  where AdID in (" + text + ")");
                this.GetAllAd();
            }
            else
            {
                this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "msg", "<script>alert('修改失败！');</script>");
            }
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        if (!base.admin.CheckAdminPower(3))
        {
            this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "alert", "<script>alert('对不起，你没有相应的权限,请跟系统超级管理员联系!');</script>");
        }
        else
        {
            string text = string.Empty;
            foreach (GridViewRow gridViewRow in this.GridView1.Rows)
            {
                CheckBox checkBox = (CheckBox)gridViewRow.Cells[0].FindControl("CheckBox1");
                if (checkBox.Checked)
                {
                    HiddenField hiddenField = (HiddenField)gridViewRow.Cells[0].FindControl("hfId");
                    text = text + hiddenField.Value + ",";
                }
            }
            if (text != string.Empty)
            {
                text = text.Substring(0, text.Length - 1);
                this.conn.Execute("delete CM_Advertisement where AdID in (" + text + ")");
                this.GetAllAd();
            }
            else
            {
                this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "msg", "<script>alert('请选择删除的数据！');</script>");
            }
        }
    }
    private string GetPage(int yushu, int page, int last, int allpage)
    {
        string text = base.Request.Url.ToString().ToLower();
        int begin = text.LastIndexOf("page=");
        int deletePoint = this.GetDeletePoint(begin, text);
        string text2 = "";
        string text3;
        if (base.Request.Url.ToString().IndexOf("?") > 0)
        {
            text3 = base.Request.Url.ToString().Remove(base.Request.Url.ToString().IndexOf("?"));
        }
        else
        {
            text3 = base.Request.Url.ToString();
        }
        for (int i = 1; i <= yushu; i++)
        {
            if (page == last && page != allpage && page % 10 != 0)
            {
                object obj = text2;
                text2 = string.Concat(new object[]
				{
					obj,
					"<li class='",
					(page == last + i - 1) ? "cur" : "",
					"'><a  href='",
					text3,
					"?page=",
					last + i,
					"'>",
					last + i,
					"</a></li>"
				});
            }
            else
            {
                if (page == last && page == allpage)
                {
                    last -= 10;
                    object obj = text2;
                    text2 = string.Concat(new object[]
					{
						obj,
						"<li class='",
						(page == last + i) ? "cur" : "",
						"'><a  href='",
						text3,
						"?page=",
						last + i,
						"'>",
						last + i,
						"</a></li>"
					});
                }
                else
                {
                    if (page == last && page != allpage && page % 10 == 0)
                    {
                        last -= 10;
                        object obj = text2;
                        text2 = string.Concat(new object[]
						{
							obj,
							"<li class='",
							(page == last + i - 1) ? "cur" : "",
							"'><a  href='",
							text3,
							"?page=",
							last + i,
							"'>",
							last + i,
							"</a></li>"
						});
                    }
                    else
                    {
                        object obj = text2;
                        text2 = string.Concat(new object[]
						{
							obj,
							"<li class='",
							(page == last + i) ? "cur" : "",
							"'><a  href='",
							text3,
							"?page=",
							last + i,
							"'>",
							last + i,
							"</a></li>"
						});
                    }
                }
            }
        }
        return text2;
    }
    private void GetPageLink()
    {
        string text = base.Request.Url.ToString().ToLower();
        int begin = text.LastIndexOf("page=");
        int deletePoint = this.GetDeletePoint(begin, text);
        string text2;
        if (base.Request.Url.ToString().IndexOf("?") > 0)
        {
            text2 = base.Request.Url.ToString().Remove(base.Request.Url.ToString().IndexOf("?"));
        }
        else
        {
            text2 = base.Request.Url.ToString();
        }
        int num = this.ValidateQueString();
        int num2 = (this.AllCount + this.ReturnDataCountForEach - 1) / this.ReturnDataCountForEach;
        if (num2 == 0)
        {
            this.ltrPage.Text = "<li class='xyue'><a  href='javascript:;'>上一页</a></li><li class='xyue'><a  href='javascript:'>下一页</a></li>";
        }
        else
        {
            if (num <= num2 && num > 0)
            {
                string text3 = "";
                text3 += ((num == 1) ? "<li class='xyue'><a  href='javascript:;'>上一页</a></li>" : string.Concat(new object[]
				{
					"<li class='xyue'><a  href='",
					text2,
					"?page=",
					num - 1,
					"'>上一页</a></li>"
				}));
                if (num > 0 && num <= 10)
                {
                    int yushu = (num2 / 10 > 0) ? 10 : num2;
                    text3 += this.GetPage(yushu, num, 0, num2);
                }
                else
                {
                    if (num > 10 && num <= 10 * (num2 / 10))
                    {
                        int last = 10 * (num / 10);
                        text3 += this.GetPage(10, num, last, num2);
                    }
                    else
                    {
                        if (num > 10 * (num2 / 10) && num <= num2)
                        {
                            int yushu = num2 % 10;
                            int last = 10 * (num2 / 10);
                            text3 += this.GetPage(yushu, num, last, num2);
                        }
                    }
                }
                if (num2 == num)
                {
                    text3 += "<li class='xyue'><a  href='javascript:'>下一页</a></li>";
                }
                else
                {
                    object obj = text3;
                    text3 = string.Concat(new object[]
					{
						obj,
						"<li class='xyue'><a  href='",
						text2,
						"?page=",
						num + 1,
						"'>下一页</a></li>"
					});
                }
                this.ltrPage.Text = text3;
            }
        }
    }
    private int GetDeletePoint(int begin, string url)
    {
        int result = begin;
        if (begin > 0)
        {
            result = begin + 1;
            for (int i = begin + 1; i < url.Length; i++)
            {
                if (url[i] == '&')
                {
                    result = i;
                }
            }
        }
        return result;
    }
    private int ValidateQueString()
    {
        string text = base.Request.QueryString["page"];
        int num = 1;
        int result;
        if (!string.IsNullOrEmpty(text))
        {
            if (!int.TryParse(text, out num))
            {
                result = 1;
            }
            else
            {
                result = num;
            }
        }
        else
        {
            result = 1;
        }
        return result;
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "del")
        {
            if (this.conn.Execute("delete CM_Advertisement where AdID=" + e.CommandArgument.ToString()) <= 0)
            {
                this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "msg", "<script>alert('删除失败！');</script>");
            }
            else
            {
                this.GetAllAd();
            }
        }
    }
}
